<?php

/**
 *  公用function 
 */

/**
 * 模板获取所有get参数，加入自己要替换的
 * @param role int 角色id
 * @example 模板url调用 {:U('Admin/News/index',getParam(array('key'=>123,'value'=>222)))}
 *  */
function getParam($key) {
    $result = $_GET;

    foreach ($key as $key => $a) {
        $result[$key] = $a;
    }

    return $result;
}

/**
 * POST 请求
 * @param string $url
 * @param array $param
 * @return string content
 */
function http_post($url, $param ) {
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
    }
    if (is_string($param)) {
        $strPOST = $param;
    } else {
        $aPOST = array();
        foreach ($param as $key => $val) {
            $aPOST[] = $key . "=" . urlencode($val);
        }
        $strPOST = join("&", $aPOST);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($oCurl, CURLOPT_POST, true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    
    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

function http_post_json($url, $json,$headerAppend=array()) {
	$header=array(
		'Content-Type: application/json; charset=utf-8',
		'Content-Length: ' . strlen($json)
	);
	
	//$header=array_merge($header, $headerAppend);
	//dump($header);exit;
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
	ob_start();
	curl_exec($ch);
	$return_content = ob_get_contents();
	ob_end_clean();

	$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
	if ($return_code == 200) {
		return $return_content;
	}
	else{
		return false;
	}
}

/**
 * GET 请求
 * @param string $url
 */
function http_get($url) {
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * 生成guid
 * */
function create_guid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $hyphen = chr(45); // "-"
    $uuid = chr(123)// "{"
            . substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12)
            . chr(125); // "}"
    return $uuid;
}

/**
 * 检查数组每项是否为空
 * @param array $data 数据数组
 * @return bool true|false
 */
function isNotEmptyArr($data) {
    foreach ($data as $a) {
        if (empty($a)) {
            return false;
        }
    }

    return true;
}

/**
 * 生成短链接
 */
function shorturl($url = '', $prefix = '', $suffix = '') {
    $base32 = array(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
        'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
        'y', 'z', '0', '1', '2', '3', '4', '5');

    $hex = md5($prefix . $url . $suffix);
    $hexLen = strlen($hex);
    $subHexLen = $hexLen / 8;
    $output = array();

    for ($i = 0; $i < $subHexLen; $i++) {
        $subHex = substr($hex, $i * 8, 8);
        $int = 0x3FFFFFFF & (1 * ('0x' . $subHex));
        $out = '';
        for ($j = 0; $j < 6; $j++) {
            $val = 0x0000001F & $int;
            $out .= $base32[$val];
            $int = $int >> 5;
        }
        $output[] = $out;
    }
    return $output;
}

/**
 * 	使用特定function对数组中所有元素做处理
 * 	@param	string	&$array		要处理的字符串
 * 	@param	string	$function	要执行的函数
 * 	@return boolean	$apply_to_keys_also		是否也应用到key上
 * 	@access public
 *
 */
function arrayRecursive(&$array, $function = "rawurlencode", $applyToKeysAlso = false) {
    static $recursiveCounter = 0;
    if (++$recursiveCounter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $applyToKeysAlso);
        } else {
            $array[$key] = $function($value);
        }

        if ($applyToKeysAlso && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursiveCounter--;
}

/**
 *  转码给移动端 
 */
function rawToPhone($array) {
    arrayRecursive($array);
    return $array;
}

/**
 *  获取主机
 */
function getHost(){
	$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	$url = $protocol.$_SERVER['HTTP_HOST'];
	return $url;
}

/**
 *  获取当前完整网址url
 */
function getHostUrl(){
	$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
	$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	return $url;
}

/**
 *
 * 返回一定位数的时间戳，多少位由参数决定
 *
 * @author 陈博
 * @param type 多少位的时间戳
 * @return 时间戳
 */
function getTimestamp($digits = false) {
	$digits = $digits > 10 ? $digits : 10;
	$digits = $digits - 10;
	if ((!$digits) || ($digits == 10))
	{
		return time();
	}
	else
	{
		return number_format(microtime(true),$digits,'','');
	}
}
